//
//  RecommendViewController.m
//  God_is_a_Girl
//
//  Created by 李继鹏 on 15/9/23.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "LJP_RecommendViewController.h"
#import "LJP_headerView.h"
#import <AFNetworking.h>
#import "LJP_RecommendModel.h"
#import "LJP_RecommendCell.h"
#import "LJP_ScrollView.h"
#import "LJP_SearchViewController.h"
#import "LJP_ DetailsViewController.h"
#import <MJRefresh.h>

@interface LJP_RecommendViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) LJP_headerView *headerView;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic,retain) NSMutableDictionary *mDic;
@property (nonatomic,retain) NSMutableArray *mainArray;
@property (nonatomic,retain) NSMutableArray *scrollArray;
@property (nonatomic,retain) NSMutableArray *searchArray;
@property (nonatomic,retain) NSString *downlaodstr;
@end

@implementation LJP_RecommendViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.alpha = 1;
    self.tabBarController.tabBar.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-49, self.view.frame.size.width, 49);
    self.navigationController.navigationBar.translucent = NO;
}
- (void)headerViewSubview
{
    self.headerView = [[LJP_headerView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    self.headerView.backgroundColor = self.mainColor;
    [self.headerView.searchButton addTarget:self action:@selector(searchButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.headerView];
}
- (void)searchButtonClicked
{
    LJP_SearchViewController *searchVC = [[LJP_SearchViewController alloc] init];
    searchVC.dataArray = self.searchArray;
    self.tabBarController.tabBar.alpha = 0;
    self.tabBarController.tabBar.frame = CGRectMake(-100, -100, 0, 0);
    [self.navigationController pushViewController:searchVC animated:YES];
}
- (void)tableViewSubview
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,64, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStyleGrouped];
    
    self.tableView.backgroundColor = [UIColor colorWithRed:251/255.0f green:247/255.0f blue:220/255.0f alpha:0.8];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.contentInset = UIEdgeInsetsMake(-18, -3, 0, -20);//偏移量
    
    self.tableView.showsVerticalScrollIndicator = NO;//关闭右划线
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    [self.tableView.header beginRefreshing];
        [self dataDandle];
        [self.tableView.header endRefreshing];
        
    }];
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
       [self.tableView.footer beginRefreshing];
        [self dataDandleFooter];
    }];
                             
//    self.loading = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    //提示文本
//    self.loading.labelText = @"正在加载...";
    //    //是否有遮罩
    //    self.loading.dimBackground = YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _mainArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJP_RecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"qwer"];
    if (cell == nil) {
        cell = [[LJP_RecommendCell alloc] initWithStyle:0 reuseIdentifier:@"qwer"];
    }
    cell.model = [self.mainArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self headerViewSubview];
    [self tableViewSubview];
    [self dataDandle];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJP__DetailsViewController *tracetwo = [[LJP__DetailsViewController alloc]init];
    LJP_RecommendModel *trace = [[LJP_RecommendModel alloc]init];
    
    trace = self.mainArray[indexPath.row];
    tracetwo.stringId = trace.traceid;
    
    [self.navigationController pushViewController:tracetwo animated:YES];
}
/* 调整Cell高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
- (void)dataDandleFooter
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:[NSString stringWithFormat:@"http://api.breadtrip.com/v5/index/?next_start=%@",self.downlaodstr] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        /* 数据处理 */
        NSArray *subjectArr = [responseObject objectForKey:@"elements"];
        self.downlaodstr = [responseObject objectForKey:@"next_start"];
//        self.mDic = [NSMutableDictionary dictionary];
//        self.mainArray = [NSMutableArray array];
//        self.scrollArray = [NSMutableArray array];
//        self.searchArray = [responseObject objectForKey:@"search_data"];
        for (NSDictionary *dic in subjectArr) {
            NSString *str1 = [[dic valueForKey:@"type"]stringValue];
            if ([str1 isEqualToString:@"4"]) {
                self.mDic = (NSMutableDictionary *)dic;
                LJP_RecommendModel *tracepage = [LJP_RecommendModel traceWithDic:dic];
                [self.mainArray addObject:tracepage];
                [self.tableView reloadData];
            }
            if ([str1 isEqualToString:@"1"]) {
                NSArray *arr = [[dic objectForKey:@"data"]firstObject];
                for (NSDictionary *dic1 in arr) {
                    NSString *str1 = [dic1 objectForKey:@"image_url"];
                    [self.scrollArray addObject:str1];
                }
            }
        }
        [self.tableView.footer endRefreshing];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
- (void)dataDandle
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://api.breadtrip.com/v5/index/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        /* 数据处理 */
        NSArray *subjectArr = [responseObject objectForKey:@"elements"];
        self.downlaodstr = [responseObject objectForKey:@"next_start"];
        self.mDic = [NSMutableDictionary dictionary];
        self.mainArray = [NSMutableArray array];
        self.scrollArray = [NSMutableArray array];
        self.searchArray = [responseObject objectForKey:@"search_data"];        
        for (NSDictionary *dic in subjectArr) {
            NSString *str1 = [[dic valueForKey:@"type"]stringValue];
            if ([str1 isEqualToString:@"4"]) {
                self.mDic = (NSMutableDictionary *)dic;
                LJP_RecommendModel *tracepage = [LJP_RecommendModel traceWithDic:dic];
                [self.mainArray addObject:tracepage];
                [self.tableView reloadData];
            }
            if ([str1 isEqualToString:@"1"]) {
                NSArray *arr = [[dic objectForKey:@"data"]firstObject];
                for (NSDictionary *dic1 in arr) {
                    NSString *str1 = [dic1 objectForKey:@"image_url"];
                    [self.scrollArray addObject:str1];
                }
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];

}
/* 分区头部高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {
        return 170;
    }
    return 0;
    
}
//轮播图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        if (self.scrollArray.count != 0) {
            LJP_ScrollView *head = [[LJP_ScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 170)];
            head.picarr = self.scrollArray;
//            [self.tableView addSubview:head];
            return head;
        }
        return nil;
        
    }else{
        
        return nil;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
